package com.tbit.uqbike.webmanager.controller;


import com.tbit.uqbike.constant.ResponseCode;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Image;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.ImageService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.FileLoadUtils;
import com.tbit.uqbike.webmanager.util.OutputUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import static com.tbit.uqbike.webmanager.controller.BaseController.getLogin;

/**
 * 图片
 *
 * @author Leon
 * 2019年11月16日 下午2:36:24
 */
@RestController
@RequestMapping("/image")
public class ImageController {
    @Resource
    private ImageService imageService;

    /* 查看图片 */
    @RequestMapping("/getByImageId")
    public void getByImageId(String imageId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.hasLength(imageId, "Image.id.notNull");

            /**从数据库加载图片*/
            Image image = imageService.getByImageId(imageId);
            if (image == null) {
                throw new BaseException("Image.notExist");
            }

            if (image.getImageContent().length > 0) {
                response.setContentType(image.getImageType());
                OutputStream out = response.getOutputStream();
                try {
                    out.write(image.getImageContent());
                } catch (Exception e) {
                    e.getMessage();
                } finally {
                    out.flush();
                    out.close();
                    image = null;
                }
            }
        } catch (BaseException e) {
            ResponseCode.printException(map, e);
            OutputUtil.renderText(request, response, map);
        }
    }


    /**
     * 上传图片
     * @param file 图片对象
     * @param accountId 区域编号
     * @param request 请求报文
     * @return 图片地址
     */
    @RequestMapping("/uploadImage")
    public JsonResult uploadImage(Integer accountId, HttpServletRequest request, MultipartFile file, String bucketName) throws Exception {
        // 参数校验
        Assert.notNull(file, "have_not_file");
        // 登录用户信息
        LoginInfo loginInfo = getLogin(request);
        if (loginInfo.type.equals(UserConstant.USER_AGENT)) {
            accountId = loginInfo.id;
        } else {
            Assert.notNull(accountId, "ver.accountId.notNull");
        }
        // 校验文件大小
        if (!FileLoadUtils.checkFileSize(file, 2, "M")) {
            throw new BaseException(SystemCodeEnum.IMAGE_SIZE_LIMIT2M);
        }
        return JsonResult.ok(imageService.uploadImage(accountId, file, bucketName));
    }

}